package question6;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @FileName Server
 * @Description
 * @Author Sword
 * @date 2024-12-24
 **/
public class Server {
    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String USER = "root";
    private static final String PASSWORD = "423538";

    public static void main(String[] args) throws IOException {
        int port = 12345;
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        BufferedReader in = null;
        PrintWriter out = null;

        try {
            serverSocket = new ServerSocket(port);
            System.out.println("服务器已启动，正在监听端口 " + port);

            while (true) {
                clientSocket = serverSocket.accept();
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                out = new PrintWriter(clientSocket.getOutputStream(), true);

                String inputLine;
                if ((inputLine = in.readLine()) != null) {
                    System.out.println("收到客户端消息: " + inputLine);
                    saveDataToDatabase(inputLine); // 将数据保存到数据库
                    String response = "服务器已接收到数据并保存: " + inputLine;
                    out.println(response);
                }
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        } finally {
            closeResources(in, out, clientSocket, serverSocket);
        }
    }

    private static void saveDataToDatabase(String data) throws SQLException {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            // 注册 JDBC 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 打开连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            // 插入数据
            String sql = "INSERT INTO websites (name, url) VALUES (?, ?)";
            pstmt = conn.prepareStatement(sql);
            String[] parts = data.split(",");
            if (parts.length == 2) {
                pstmt.setString(1, parts[0].trim());
                pstmt.setString(2, parts[1].trim());
                pstmt.executeUpdate();
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            closeDBResources(pstmt, conn);
        }
    }

    private static void closeResources(BufferedReader in, PrintWriter out, Socket clientSocket, ServerSocket serverSocket) {
        try {
            if (in != null) in.close();
            if (out != null) out.close();
            if (clientSocket != null) clientSocket.close();
            if (serverSocket != null) serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void closeDBResources(PreparedStatement pstmt, Connection conn) {
        try {
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
